Skip to content

fix(telegram): handle caption for document and video messages (#7000)#7019

Closed
Yaohua-Leo wants to merge 1 commit intoAstrBotDevs:masterfrom
Yaohua-Leo:fix/7000-telegram-caption
Closed

fix(telegram): handle caption for document and video messages (#7000)#7019
Yaohua-Leo wants to merge 1 commit intoAstrBotDevs:masterfrom
Yaohua-Leo:fix/7000-telegram-caption

Conversation

@Yaohua-Leo
Copy link
Contributor

@Yaohua-Leo Yaohua-Leo commented Mar 26, 2026

Summary

Fixes #7000

Changes

  • Add caption handling to document branch
  • Add caption handling to video branch
  • Handle caption_entities for mention support

Summary by Sourcery

Handle captions and mentions for Telegram document and video messages.

Bug Fixes:

  • Parse and attach captions for incoming Telegram document messages.
  • Parse and attach captions for incoming Telegram video messages.
  • Convert caption mention entities into internal mention components when present in captions.

…tDevs#7000)

- Add caption handling to document branch
- Add caption handling to video branch
- Handle caption_entities for @mention support

Fixes AstrBotDevs#7000
@auto-assign auto-assign bot requested review from Raven95676 and Soulter March 26, 2026 15:24
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 26, 2026
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The caption and caption_entities handling logic for document and video messages is duplicated; consider extracting this into a small helper function to keep the branches consistent and easier to maintain.
  • If there is already shared logic elsewhere for handling entities (e.g., mentions) in regular message text, it may be cleaner to reuse that rather than reimplementing the mention extraction for captions here.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The caption and caption_entities handling logic for document and video messages is duplicated; consider extracting this into a small helper function to keep the branches consistent and easier to maintain.
- If there is already shared logic elsewhere for handling entities (e.g., mentions) in regular message text, it may be cleaner to reuse that rather than reimplementing the mention extraction for captions here.

## Individual Comments

### Comment 1
<location path="astrbot/core/platform/sources/telegram/tg_adapter.py" line_range="489" />
<code_context>
                 message.message.append(
                     Comp.File(file=file_path, name=file_name, url=file_path)
                 )
+            if update.message.caption:
+                message.message_str = update.message.caption
+                message.message.append(Comp.Plain(message.message_str))
</code_context>
<issue_to_address>
**issue (complexity):** Consider extracting the repeated caption and mention handling into a shared helper used by both the document and video branches to avoid duplication.

You can remove the duplicated caption logic in the `document` and `video` branches by extracting it into a small helper. That keeps media-specific handling localized and makes future changes to caption parsing one‑shot.

For example:

```python
def _append_caption_components(message: Message, tg_message: telegram.Message) -> None:
    if tg_message.caption:
        message.message_str = tg_message.caption
        message.message.append(Comp.Plain(message.message_str))

    if tg_message.caption_entities:
        for entity in tg_message.caption_entities:
            if entity.type == "mention":
                name = message.message_str[
                    entity.offset + 1 : entity.offset + entity.length
                ]
                message.message.append(Comp.At(qq=name, name=name))
```

Then use it in both branches:

```python
if update.message.document:
    # existing document/file handling...
    if file_path is not None:
        message.message.append(Comp.File(file=file_path, name=file_name, url=file_path))
    _append_caption_components(message, update.message)

elif update.message.video:
    # existing video handling...
    if file_path is not None:
        message.message.append(Comp.Video(file=file_path, path=file.file_path))
    _append_caption_components(message, update.message)
```

This keeps behavior identical but removes duplicated control flow and ensures any future change to caption/mention handling is implemented in a single place.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

message.message.append(
Comp.File(file=file_path, name=file_name, url=file_path)
)
if update.message.caption:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the repeated caption and mention handling into a shared helper used by both the document and video branches to avoid duplication.

You can remove the duplicated caption logic in the document and video branches by extracting it into a small helper. That keeps media-specific handling localized and makes future changes to caption parsing one‑shot.

For example:

def _append_caption_components(message: Message, tg_message: telegram.Message) -> None:
    if tg_message.caption:
        message.message_str = tg_message.caption
        message.message.append(Comp.Plain(message.message_str))

    if tg_message.caption_entities:
        for entity in tg_message.caption_entities:
            if entity.type == "mention":
                name = message.message_str[
                    entity.offset + 1 : entity.offset + entity.length
                ]
                message.message.append(Comp.At(qq=name, name=name))

Then use it in both branches:

if update.message.document:
    # existing document/file handling...
    if file_path is not None:
        message.message.append(Comp.File(file=file_path, name=file_name, url=file_path))
    _append_caption_components(message, update.message)

elif update.message.video:
    # existing video handling...
    if file_path is not None:
        message.message.append(Comp.Video(file=file_path, path=file.file_path))
    _append_caption_components(message, update.message)

This keeps behavior identical but removes duplicated control flow and ensures any future change to caption/mention handling is implemented in a single place.

@dosubot dosubot bot added the area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. label Mar 26, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Telegram adapter in tg_adapter.py to support captions and mention entities for messages containing files or videos. The changes ensure that caption text is captured as plain text and mentions are correctly parsed into 'At' components. I have no feedback to provide.

@Soulter
Copy link
Member

Soulter commented Mar 27, 2026

#7020 fixed

@Soulter Soulter closed this Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]Telegram 适配器对附件消息的 caption 处理不一致

2 participants